package com.kool.kadmin.mqtt.service;

import com.alibaba.fastjson.JSON;
import com.kool.kadmin.client.KmetricsClient;
import com.kool.kadmin.client.KreportClient;
import com.kool.kadmin.client.request.GetClientTopDailyReportReq;
import com.kool.kadmin.client.request.GetGlobalDailyReportReq;
import com.kool.kadmin.client.request.GetTopicTopDailyReportReq;
import com.kool.kadmin.client.request.GetUserTopDailyReportReq;
import com.kool.kadmin.client.vo.*;
import com.kool.kadmin.common.util.DateUtil;
import com.kool.kadmin.mqtt.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.temporal.Temporal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author luyu
 * @date 2021-04-19 20:29
 */
@Service
@Slf4j
public class DashboardService {
    @Resource
    private KmetricsClient kmetricsClient;
    @Resource
    private KreportClient kreportClient;

    public static final int TOP_N = 3;

    /**
     * 查询dashboard实时指标
     *
     * @return
     */
    public DashboardRealtimeMetrics getRealtimeMetrics() {
        DashboardRealtimeMetrics metrics = new DashboardRealtimeMetrics();
        MetricsVo metricsVo = kmetricsClient.getGlobalCnt();
        if (metricsVo != null) {
            metrics = JSON.parseObject(JSON.toJSONString(metricsVo), DashboardRealtimeMetrics.class);
            Long receiveTotal = metricsVo.getReceiveTotal();
            if (receiveTotal != null && receiveTotal > 0L) {
                Long receiveSuccess = metricsVo.getReceiveSuccess();
                BigDecimal receiveSuccessRate = new BigDecimal(receiveSuccess)
                        .multiply(new BigDecimal(100))
                        .divide(new BigDecimal(receiveTotal), 2, BigDecimal.ROUND_HALF_UP);
                metrics.setReceiveSuccessRate(receiveSuccessRate.doubleValue());
            }
            Long sendTotal = metricsVo.getSendTotal();
            if (sendTotal != null && sendTotal > 0L) {
                Long sendSuccess = metricsVo.getSendSuccess();
                BigDecimal sendSuccessRate = new BigDecimal(sendSuccess)
                        .multiply(new BigDecimal(100))
                        .divide(new BigDecimal(sendTotal), 2, BigDecimal.ROUND_HALF_UP);
                metrics.setSendSuccessRate(sendSuccessRate.doubleValue());
            }
        }
        return metrics;
    }

    /**
     * 查询dashboard一个月的日报表
     *
     * @return
     */
    public List<DashboardDailyReport> getDailyReport() {
        int fromDay = DateUtil.toIntDay(DateUtil.getDateAfter(new Date(), -1));
        int toDay = DateUtil.toIntDay(new Date());


        GetGlobalDailyReportReq request = new GetGlobalDailyReportReq();
        request.setFromDay(fromDay);
        request.setToDay(toDay);
        List<DailyReportVo> dailyReportVos = kreportClient.getGlobalDailyReport(request);
        if (dailyReportVos == null) {
            return null;
        } else {
            return dailyReportVos.stream().map(item -> JSON.parseObject(JSON.toJSONString(item), DashboardDailyReport.class)).collect(Collectors.toList());
        }
    }

    /**
     * 查询dashboard主题昨日top统计
     *
     * @return
     */
    public DashboardTopicTop getTopicTop() {
        int yesterday = DateUtil.toIntDay(DateUtil.getDateAfter(new Date(), -1));

        GetTopicTopDailyReportReq request = new GetTopicTopDailyReportReq();
        request.setDay(yesterday);
        request.setTop(TOP_N);
        TopicTopReport topicTopReport = kreportClient.getTopicTopDailyReport(request);

        DashboardTopicTop dashboardTopicTop = new DashboardTopicTop();
        if (topicTopReport != null) {
            dashboardTopicTop.setTopicFilterSubscriberCntTop(topicTopReport.getTopicFilterSubscriberCntTop());
            dashboardTopicTop.setTopicReceiveSuccessPublishCntTop(topicTopReport.getTopicReceiveSuccessPublishCntTop());
            dashboardTopicTop.setTopicSendSuccessPublishCntTop(topicTopReport.getTopicSendSuccessPublishCntTop());
        }
        return dashboardTopicTop;
    }

    /**
     * 查询dashboard用户昨日top统计
     *
     * @return
     */
    public DashboardUserTop getUserTop() {
        int yesterday = DateUtil.toIntDay(DateUtil.getDateAfter(new Date(), -1));
        GetUserTopDailyReportReq request = new GetUserTopDailyReportReq();
        request.setDay(yesterday);
        request.setTop(TOP_N);

        UserTopReport userTopReport = kreportClient.getUserTopDailyReport(request);
        DashboardUserTop dashboardUserTop = new DashboardUserTop();
        if (userTopReport != null) {
            dashboardUserTop.setUserReceiveSuccessCntTop(userTopReport.getUserReceiveSuccessCntTop());
            dashboardUserTop.setUserSendSuccessCntTop(userTopReport.getUserSendSuccessCntTop());
        }
        return dashboardUserTop;
    }

    /**
     * 查询dashboard客户端昨日top统计
     *
     * @return
     */
    public DashboardClientTop getClientTop() {
        int yesterday = DateUtil.toIntDay(DateUtil.getDateAfter(new Date(), -1));

        GetClientTopDailyReportReq request = new GetClientTopDailyReportReq();
        request.setDay(yesterday);
        request.setTop(TOP_N);

        ClientTopReport clientTopReport = kreportClient.getClientTopDailyReport(request);
        DashboardClientTop dashboardClientTop = new DashboardClientTop();
        if (clientTopReport != null) {
            dashboardClientTop.setClientReceiveSuccessCntTop(clientTopReport.getClientReceiveSuccessCntTop());
            dashboardClientTop.setClientSendSuccessCntTop(clientTopReport.getClientSendSuccessCntTop());
            dashboardClientTop.setRemoteAddressReceiveSuccessCntTop(clientTopReport.getRemoteAddressReceiveSuccessCntTop());
            dashboardClientTop.setRemoteAddressSendSuccessCntTop(clientTopReport.getRemoteAddressSendSuccessCntTop());
        }
        return dashboardClientTop;
    }
}
